package com.wonders.bigdata.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Description: 利用文件通道和buffer读取文件<br>
 *
 * @author: XB
 * @date: 2021/5/6 9:38
 */
public class NIOFilechannel02 {
    public static void main(String[] args) throws Exception {
        File file = new File("/file/netty/file01.txt");
        // 创建一个输入流->channel
        FileInputStream fileInputStream = new FileInputStream(file);
        // 通过fileInputStream获取对应frfilechannel
        FileChannel fileChannel = fileInputStream.getChannel();
        // 创建一个缓冲区ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        // 将文件输入流读入缓冲区
        fileChannel.read(byteBuffer);
        // 将字节转成字符串
        System.out.println(new String(byteBuffer.array()));
        fileChannel.close();
        fileInputStream.close();
    }
}
